/**
 * Project: derbytest
 * 
 * File Created at 2011-1-19
 * $Id: SonarTest.java 121 2011-01-20 12:10:31Z ymkyve501@163.com $
 * 
 * Copyright 1999-2100 Alibaba.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */
package com.complex.derby;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * sonar默认使用的就是derby数据库,这段代码用来操作sonar的derby
 * 前提是,启动了sonar
 * 
 * @author gaoliang
 * @date 2011-1-19
 */
public class SonarTest {
    public static void main(String[] args) {
        try { // load the driver
              //Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();

            Connection conn = null;
            Properties props = new Properties();
            props.put("user", "sonar");
            props.put("password", "sonar");
            //create and connect the database named helloDB
            //只能以localhost或127.0.0.1形式,不能以具体IP形式,会提示 Connection refused: connect
            conn = DriverManager
                    .getConnection("jdbc:derby://localhost:1527/sonar;user=sonar;password=sonar");

            conn.setAutoCommit(false);

            // create a table and insert two records
            Statement s = conn.createStatement();
            s.execute("delete from sonar.snapshots where project_id = 1815 and created_at < TIMESTAMP('2010-12-29','00:00:00')");
            // list the two records
            ResultSet rs = s
                    .executeQuery("select project_id,status from sonar.snapshots where project_id = 1815 and created_at < TIMESTAMP('2010-12-29','00:00:00')");

            //s.executeQuery("select name from sonar.projects where id = 1815");
            while (rs.next()) {
                StringBuilder builder = new StringBuilder();
                builder.append("result:");
                builder.append("project_id=" + rs.getInt(1) + ",");
                builder.append("status=" + rs.getString(2));
                System.out.println(builder.toString());
            }

        } catch (Throwable e) {
            // handle the exception
            System.out.println(e);
        }
        System.out.println("SimpleApp finished");
    }
}
